package Model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UserDAO {

    public UserDAO() {
    }
//	public boolean save(User user) {
//		boolean result = false;
//
//		Connection con = DAO.getConnection();
//		try {
//			Statement state = con.createStatement();
//			String sql = "insert into dangky(loginName,password,email,fullName)" + " values('"
//
//					+ user.getLoginName() + "', '"
//					+ user.getPass()      + "', '"
//					+ user.getEmail()     + "', '"
//                                        + user.getFullName()  +
//					   "')";
//
//			int number = state.executeUpdate(sql);
//			if (number > 0) {
//				result = true;
//			}
//
//			con.close();
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//
//		return result;
//	}
//
//    public boolean checkLogin(User user) {
//        boolean result = false;
//        Connection con = new DAO().getConnection();
//        try {
//            Statement state = con.createStatement();
//            String sql = "select * from dangky where loginName='" + user.getLoginName()
//                    + "'and password='" + user.getPassword() + "'";
//            ResultSet rs = state.executeQuery(sql);
//            while (rs.next() == true) {
//                result = true;
//            }
//            con.close();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//
//
//        return result;
//    }

    public static boolean checkLoginName(User user) {
        boolean result = false;

        Connection con = DAO.getConnection();
        try {
            String sql = "select * from shoppingonline.user where username= ?";
            PreparedStatement state = con.prepareStatement(sql);
//			String sql = "select * from dangky where loginName='" + user.getLoginName() + "'";
            state.setString(1, user.getUsername());
            ResultSet rs = state.executeQuery();
            String role = null;
            while (rs.next()) {
                result = true;
                role = rs.getString("role");
            }
            user.setRole(role);
            con.close();
            
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println(user.getRole());

        return result;
    }

    public static boolean checkLogin(User user) {
        boolean result = false;
        Connection con = DAO.getConnection();
        try {
            String sql = "select * from shoppingonline.user where username= ? and password= ?";
            PreparedStatement state = con.prepareStatement(sql);
            state.setString(1, user.getUsername());
            state.setString(2, user.getPassword());
            String role = null, email = null, name = null;
            ResultSet rs = state.executeQuery();
            while (rs.next() == true) {
                role = rs.getString("role");
                email = rs.getString("email");
                name = rs.getString("fullname");
                result = true;
            }
            user.setRole(role);
            user.setEmail(email);
            user.setFullname(name);
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return result;
    }

    public static boolean checkEmail(User user) {
        boolean result = false;
        Connection con = DAO.getConnection();
        try {
            String sql = "select * from user where email = ?";
            PreparedStatement state = con.prepareStatement(sql);
            state.setString(1, user.getEmail());
            ResultSet rs = state.executeQuery();
            while (rs.next()) {
                result = true;
            }
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
    public boolean updateRole(String username, String role){
        boolean res = false;
        Connection con = DAO.getConnection();
        try {
            String sql = "update user set role=? where username=?";
            PreparedStatement state = con.prepareStatement(sql);
            state.setString(1, role);
            state.setString(2, username);
           
            int isRes = state.executeUpdate();
            if(isRes>0) res = true;
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;                
    }
    
    public User layRole(String username){
        String res = null;
        User user = new User();
        Connection con = DAO.getConnection();
        try {
            String sql = "select role, fullname from user where username=?";
            PreparedStatement state = con.prepareStatement(sql);            
            state.setString(1, username);           
            ResultSet rs = state.executeQuery();
            while(rs.next()){
                user.setRole(rs.getString("role"));
                user.setFullname(rs.getString("fullname"));
                
            }
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return user;                
    }
    

    public static void main(String[] args) {
        User u = new User();
        u.setUsername("nani");
        u.setPassword("123");
        System.out.println(UserDAO.checkLogin(u));
        System.out.println(u.getRole());
        System.out.println(new UserDAO().updateRole("a", "admin"));
        System.out.println(new UserDAO().layRole("a"));
    }

   }
